package com.midea.thread;

import java.util.concurrent.*;

/**
 * 通过线程池来创建线程
 * 实现带返回值的多线程创建
 * */
public class TestCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService es = Executors.newFixedThreadPool(2);

        Future<Integer> f1 = es.submit(new MyCallable(100));
        Future<Integer> f2 = es.submit(new MyCallable(50));
        System.out.println(f1.get());
        System.out.println(f2.get());


    }
}
class MyCallable implements Callable<Integer>{
    private int num;
    public MyCallable(int num){
        this.num = num;
    }

    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0;i<num;i++){
            sum = sum + i;
        }
        return sum;
    }
}
